package com.wz.wzliving.commodity.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wz.wzliving.commodity.dao.SpuInfoDao;
import com.wz.wzliving.commodity.entity.SpuInfoEntity;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzcommon.utils.Query;
import com.wzcommon.utils.PageUtils;
import com.wzcommon.utils.R;

import com.wz.wzliving.commodity.dao.SkuInfoDao;
import com.wz.wzliving.commodity.entity.SkuInfoEntity;
import com.wz.wzliving.commodity.service.SkuInfoService;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuInfo(SkuInfoEntity skuInfoEntity) {
        this.baseMapper.insert(skuInfoEntity);
    }

    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {
        QueryWrapper<SkuInfoEntity> wrapper = new QueryWrapper<>();
//按检索条件-spu 名字
        String key = (String) params.get("key");
        if(!StringUtils.isEmpty(key)){
            wrapper.and((w)->{ //注意这里的 and 会把后面两个条件括起来
                w.eq("sku_id",key).or().like("sku_name",key);
            });
        }

//加入品牌 id
        String brandId = (String) params.get("brandId");
        if(!StringUtils.isEmpty(brandId)&&!"0".equalsIgnoreCase(brandId)){
            wrapper.eq("brand_id",brandId);
        }
//加入分类 id
        String catelogId = (String) params.get("catelogId");
        if(!StringUtils.isEmpty(catelogId)&&!"0".equalsIgnoreCase(catelogId)){
            wrapper.eq("catalog_id",catelogId);
        }
        String min = (String) params.get("min");
        if(!StringUtils.isEmpty(min)){
            wrapper.ge("price",min);
        }
        String max = (String) params.get("max");
//校验传递的价格范围合理性, 如果 max 有值,并且大于 0，
//才有必要封装到查询条件
        if(!StringUtils.isEmpty(max) ){
            try{
                BigDecimal bigDecimal = new BigDecimal(max);
                if(bigDecimal.compareTo(new BigDecimal("0"))==1){
                    wrapper.le("price",max);
                }
            }catch (Exception e){

            }
        }
//分页查询
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params), wrapper
        );
        return new PageUtils(page);
    }
    @Resource
    private SpuInfoDao spuInfoDao;
    @Override
    public PageUtils querySearchPageByCondition
            (Map<String, Object> params) {
        params.put("limit", "2");
        //先查出已经上架的 SpuInfoEntity
        List<SpuInfoEntity> spuInfoEntities = spuInfoDao.selectList(new

                QueryWrapper<SpuInfoEntity>().eq("publish_status", 1));
//收集上架的 SpuInfoEntity id
        List<Long> spuInfoEntityIdCollect = spuInfoEntities.stream().map(item -> {
            return item.getId();
        }).collect(Collectors.toList());
        QueryWrapper<SkuInfoEntity> queryWrapper = new QueryWrapper<>();
//将 spuInfoEntityIdCollect 加入分页查询条件
        if (spuInfoEntityIdCollect != null && spuInfoEntityIdCollect.size() > 0) {
            queryWrapper.in("spu_id", spuInfoEntityIdCollect);
        } else {
//没有任何上架的家居商品, 构造一个结果返回，注意不能直接返回 null
            IPage<SkuInfoEntity> page = new Page<>();
            page.setPages(0l);
            page.setCurrent(0l);
            page.setRecords(null);
            page.setTotal(0l);
            page.setSize(0l);
            return new PageUtils(page);
        }

//带上查询条件
        String key = (String) params.get("keyword");
        if(!StringUtils.isEmpty(key)){
            queryWrapper.and((wrapper)->{
                wrapper.eq("sku_id",key).or().like("sku_name",key);
            });
        }
//带上分类[第三级分类]
        String catelogId = (String) params.get("catalog3Id");
        if(!StringUtils.isEmpty(catelogId)&&!"0".equalsIgnoreCase(catelogId)){
            queryWrapper.eq("catalog_id",catelogId);
        }
//带上品牌
        String brandId = (String) params.get("brandId");
        if(!StringUtils.isEmpty(brandId)&&!"0".equalsIgnoreCase(brandId)){
            queryWrapper.eq("brand_id",brandId);
        }
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),

                queryWrapper
        );
        return new PageUtils(page);
    }

}